package com.bawei.a2010douyin.week3.ui.main;

import com.bawei.a2010douyin.base.mvp.BasePresenter;
import com.bawei.a2010douyin.db.dao.DBUtils;
import com.bawei.a2010douyin.entity.BaseResponse;
import com.bawei.a2010douyin.entity.VideoEntity;
import com.bawei.a2010douyin.week3.entity.Video;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MainPresenter extends BasePresenter<MainContract.IMainView> implements MainContract.IMainPresenter {
    private MainContract.IMainModel mIMainModel;
    public MainPresenter() {
        mIMainModel = new MainModel();
    }

    @Override
    public void video(int currentPage, int pageSize) {
        mIMainModel.video(currentPage, pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResponse<List<VideoEntity>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseResponse<List<VideoEntity>> listBaseResponse) {
                        int code = listBaseResponse.getCode();//响应吗
                        List<VideoEntity> data = listBaseResponse.getData();//数据
                        if(isAttach()){
                           if(code == 200){//成功
                               getView().showVideo(data);




                           }else{//失败
                               getView().onError();
                           }
                       }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(isAttach()){
                            getView().onError();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
